package Test;

import org.junit.jupiter.api.Test;

import static org.junit.Assert.*;

public class testSort {

    //使用冒泡排序进行测试
    @Test
    public void sort1Test(){

        int [] arr1 = {1,2,3,4,8};
        System.out.println("1.条件覆盖");
        System.out.println("测试数据，其中执行条件if中的false");
        System.out.print("  输入:");

        dataIn(arr1);
        sort(arr1);
        assertArrayEquals(new int[] {1,2,3,4,8},arr1);
        System.out.println();


        System.out.print("  输出:");
        print(arr1);

    }


    @Test
    public void sort2Test(){
        int [] arr2 = {8,5,4,3};
        System.out.println("1.条件覆盖");
        System.out.println("测试数据，执行条件if中的True");
        System.out.print("  输入:");

        dataIn(arr2);
        sort(arr2);
        assertArrayEquals(new int[] {3,4,5,8},arr2);
        System.out.println();

        System.out.print("  输出:");
        print(arr2);
    }

    @Test
    public void sort3Test(){
        int [] arr3 = {8,2,3,4,7};
        System.out.println("1.条件覆盖");
        System.out.println("测试数据，其中至少有一个逆序对");
        System.out.print("  输入:");

        dataIn(arr3);
        sort(arr3);
        assertArrayEquals(new int[] {2,3,4,7,8},arr3);
        System.out.println();

        System.out.print("  输出:");
        print(arr3);
    }

    @Test
    public void sort4Test(){
        int [] arr4 = {1};
        System.out.println("1.条件覆盖");
        System.out.println("输入单个元素");
        System.out.print("  输入:");

        dataIn(arr4);
        sort(arr4);
        assertArrayEquals(new int[]{1},arr4);
        System.out.println();

        System.out.print("  输出:");
        print(arr4);
    }

//    @Test
//    public void sort5Test(){
//        int [] arr5 = {8,7,5,2,1};
//        System.out.println("1.条件覆盖");
//        System.out.println("所有元素都是降序排列");
//        System.out.print("  输入:");
//
//        dataIn(arr5);
//        sort(arr5);
//        assertArrayEquals(new int[]{1,2,5,7,8},arr5);
//        System.out.println();
//
//        System.out.print("  输出:");
//        print(arr5);
//    }
@Test
public void sort5Test(){
    int [] arr5 = {};
    System.out.println("1.条件覆盖");
    System.out.println("输入0个元素");
    System.out.print("  输入:");

    dataIn(arr5);
    sort(arr5);
    assertArrayEquals(new int[]{},arr5);
    System.out.println();

    System.out.print("  输出:");
    print(arr5);
}

    @Test
    public void sort6Test(){
        int [] arr6 = {1,3,5,2,1};
        System.out.println("2.判定覆盖");
        System.out.println("输入包含至少一个逆序对");
        System.out.print("  输入:");

        dataIn(arr6);
        sort(arr6);
        assertArrayEquals(new int[]{1,1,2,3,5},arr6);
        System.out.println();

        System.out.print("  输出:");
        print(arr6);
    }

    @Test
    public void sort7Test(){
        int [] arr7 = {5,6,7,8,9};
        System.out.println("2.判定覆盖");
        System.out.println("所有元素都是升序");
        System.out.print("  输入:");

        dataIn(arr7);
        sort(arr7);
        assertArrayEquals(new int[]{5,6,7,8,9},arr7);
        System.out.println();

        System.out.print("  输出:");
        print(arr7);
    }

    private static void print(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+" ");
        }
    }

    public static void sort(int[] arr) {
        //进行排序
        if(arr.length == 1){
            return;
        }
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length - 1 - i; j++) {
                if(arr[j] > arr[j+1]){
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j+1] = temp;
                }
            }
        }
    }

    private void dataIn(int[] arr) {
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+" ");
        }
    }

}


